package com.obaamilos.msstore;

import com.clarkparsia.pellet.sparqldl.jena.SparqlDLExecutionFactory;
//import com.clarkparsia.sparqlowl.parser.arq.ARQTerpParser;
//import com.clarkparsia.sparqlowl.parser.arq.TerpSyntax;
import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.Model;
//import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.tdb.TDBFactory;

public class teste_msstore {

	public static void consultaTDB() {
		try  {  
			Dataset ds = TDBFactory.createDataset("C:/tdbteste/tdb1");
			Model model = ds.getDefaultModel();

			//DATA_BASE_MODEL = TDBFactory.cre .createModel(diretorio);
			//MODEL_JENA = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC,DATA_BASE_MODEL); //ARQUIVO

			//m = MODEL_JENA;

			// Now read the query file into a query object
			// Important: specifying that the query is in Terp syntax
			//com.hp.hpl.jena.query.Query q = QueryFactory.create(getQuery(), TerpSyntax.getInstance());
			
			//String mConsulta = "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>"+
			//		System.getProperty("line.separator")+
			//		"PREFIX ca: <http://www.semanticweb.org/controle_acesso#>"+
			//		System.getProperty("line.separator")+
			//		"SELECT ?r ?m"+
			//		System.getProperty("line.separator")+
			//		"WHERE { ?u a ca:User . ?u ca:loginIs \"julia\"^^xsd:string . ?u ca:hasAccess ?a  . ?a ca:hasResource ?r . ?a ca:hasAction ?m }";

			String mConsulta = "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>"+
					System.getProperty("line.separator")+
					"PREFIX ca: <http://www.semanticweb.org/controle_acesso#>"+
					System.getProperty("line.separator")+
					"SELECT ?u ?p ?l "+
					System.getProperty("line.separator")+
					//"WHERE { ?u a ca:User . ?u ca:loginIs \"pedro\"^^xsd:string . ?u ca:passwordIs ?p }";
					"WHERE { ?u a ca:User . ?u ca:loginIs ?l . ?u ca:passwordIs ?p }";
			
			com.hp.hpl.jena.query.Query q = QueryFactory.create(mConsulta);

			// Create a SPARQL-DL query execution for the given query and
			// ontology model
			QueryExecution qe = SparqlDLExecutionFactory.create(q, model);

			// We want to execute a SELECT query, do it, and return the result set
			ResultSet rs = qe.execSelect();

			// return result as XML
			//setResult(ResultSetFormatter.asXMLString(rs));
			//setResult(ResultSetFormatter.asText(rs));
			
			ResultadoQuery mresul = new ResultadoQuery(rs);

			System.out.println(mresul.toString());
			
			// Close the dataset.
			ds.close();
		} catch (Exception e) {
			System.out.println("Erro Query: " + e.getMessage());
		} finally {
			//close connection with TDB
			//DATA_BASE_MODEL.close();
			//MODEL_JENA.close(); 
		}
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
    	System.out.println("Compilou...");
    	consultaTDB();
	}

}
